package com.atguigu.daijia.customer.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.customer.mapper.CustomerInfoMapper;
import com.atguigu.daijia.customer.mapper.CustomerLoginLogMapper;
import com.atguigu.daijia.customer.service.CustomerInfoService;
import com.atguigu.daijia.model.entity.customer.CustomerInfo;
import com.atguigu.daijia.model.entity.customer.CustomerLoginLog;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoMapper, CustomerInfo> implements CustomerInfoService {

	@Autowired
	private WxMaService wxMaService;

	@Autowired
	private CustomerInfoMapper customerInfoMapper;

	@Autowired
	private CustomerLoginLogMapper customerLoginLogMapper;

	/**
	 * 条件：
	 *      1、前端开发者appid与服务器端appid一致
	 *      2、前端开发者必须加入开发者
	 * @param code
	 * @return
	 */
	@Transactional(rollbackFor = {Exception.class})
	@Override
	public Long login(String code) {

		//1.获取code值,使用微信工具包对象,获取微信唯一标识openid
		String openId = null;
		try{
			WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
			openId = sessionInfo.getOpenid();
			log.info("【小程序授权】openId={}", openId);
		} catch (WxErrorException e) {
			throw new RuntimeException(e);
		}

		//2.根据openid查询数据库表,判断是否第一次登录
		//如果openid不存在返回null,如果存在返回一条记录
		LambdaQueryWrapper<CustomerInfo> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(CustomerInfo::getWxOpenId,openId);
		CustomerInfo customerInfo = customerInfoMapper.selectOne(wrapper);
		//3.如果第一次登录,添加信息到用户表
		if(null == customerInfo) {
			customerInfo = new CustomerInfo();
			customerInfo.setNickname(String.valueOf(System.currentTimeMillis()));
			customerInfo.setAvatarUrl("https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
			customerInfo.setWxOpenId(openId);
			this.save(customerInfo);
		}

		//4.记录登录日志
		CustomerLoginLog customerLoginLog = new CustomerLoginLog();
		customerLoginLog.setCustomerId(customerInfo.getId());
		customerLoginLog.setMsg("小程序登录");
		customerLoginLogMapper.insert(customerLoginLog);
		return customerInfo.getId();
	}

	//获取客户登录信息
	@Override
	public CustomerLoginVo getCustomerInfo(Long customerId) {
		//1 根据用户id查询用户信息
		CustomerInfo customerInfo = customerInfoMapper.selectById(customerId);

		//2 封装到CustomerLoginVo
		CustomerLoginVo customerLoginVo = new CustomerLoginVo();
		//customerLoginVo.setNickname(customerInfo.getNickname());
		BeanUtils.copyProperties(customerInfo,customerLoginVo);

		//@Schema(description = "是否绑定手机号码")
		//    private Boolean isBindPhone;
		String phone = customerInfo.getPhone();
		boolean isBindPhone = StringUtils.hasText(phone);
		customerLoginVo.setIsBindPhone(isBindPhone);

		//3 CustomerLoginVo返回
		return customerLoginVo;
	}

	////更新客户微信手机号码
	@Override
	public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
		//1 根据code值获取微信绑定手机号码
		try {
			WxMaPhoneNumberInfo phoneNoInfo =
					wxMaService.getUserService().getPhoneNoInfo(updateWxPhoneForm.getCode());
			String phoneNumber = phoneNoInfo.getPhoneNumber();

			//更新用户信息
			Long customerId = updateWxPhoneForm.getCustomerId();
			CustomerInfo customerInfo = customerInfoMapper.selectById(customerId);
			customerInfo.setPhone(phoneNumber);
			customerInfoMapper.updateById(customerInfo);

			return true;
		} catch (WxErrorException e) {
			throw new GuiguException(ResultCodeEnum.DATA_ERROR);
		}
	}

	//获取客户OpenId
	@Override
	public String getCustomerOpenId(Long customerId) {
		LambdaQueryWrapper<CustomerInfo> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(CustomerInfo::getId,customerId);
		CustomerInfo customerInfo = customerInfoMapper.selectOne(wrapper);
		return customerInfo.getWxOpenId();
	}
}
